<template>
    <!--
          作者：luoyiming
          时间：2019-10-25
          描述：权限-管理员列表-添加管理员
      -->
    <el-dialog width="40%" title="添加语言" :visible.sync="dialogVisible" @close="dialogFormVisible" :close-on-click-modal="false" :close-on-press-escape="false">
      <!--form表单-->
      <el-form size="small" ref="form" :model="form" :rules="formRules" :label-width="formLabelWidth">
        <el-form-item label="语言名称" prop="name"><el-input v-model="form.name" placeholder="请输入语言名称"></el-input></el-form-item>
        <el-form-item label="字段名称" prop="varName"><el-input v-model="form.varName" placeholder="请输入字段名称必须为英文"></el-input></el-form-item>
        <el-form-item label="后端语言代码" prop="backCode"><el-input v-model="form.backCode" placeholder="请输入后端语言代码"></el-input></el-form-item>
        <el-form-item label="前端语言代码" prop="frontCode"><el-input v-model="form.frontCode" placeholder="请输入前端语言代码"></el-input></el-form-item>
        <el-form-item label="备注" prop="remark"><el-input v-model="form.remark" placeholder="请输入前端语言代码"></el-input></el-form-item>
        <el-form-item label="是否默认" prop="isDefault">
            <el-switch v-model="form.isDefault"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit" :loading="loading">确 定</el-button>
      </div>
    </el-dialog>
  </template>
  
  <script>
  import LangApi from '@/api/lang.js';
  export default {
    data() {
      return {
        /*左边长度*/
        formLabelWidth: '120px',
        /*是否显示*/
        loading: false,
        /*是否显示*/
        dialogVisible: false,
        /*form表单对象*/
        form: {
            name: '',
            varName:'',
            backCode:'',
            frontCode:'',
            remark:'',
            isDefault:false,
        },
        /*form验证*/
        formRules: {
            name: [
                {
                required: true,
                message: ' ',
                trigger: 'blur'
                }
            ],
            varName: [
                {
                required: true,
                message: ' ',
                trigger: 'blur'
                }
            ],
            backCode: [
                {
                required: true,
                message: ' ',
                trigger: 'blur'
                }
            ],
            frontCode: [
                {
                required: true,
                message: ' ',
                trigger: 'blur'
                }
            ],
            remark: [
                {
                required: true,
                message: ' ',
                trigger: 'blur'
                }
            ],
            isDefault: [
                {
                required: true,
                message: ' ',
                trigger: 'blur'
                }
            ]
        }
      };
    },
    props: ['open'],
    watch: {
      open: function(n, o) {
        if (n != o) {
          this.dialogVisible = this.open;
          this.form={
            name: '',
            varName:'',
            backCode:'',
            frontCode:'',
            remark:'',
            isDefault:false,
          }
        }
      }
    },
    created() {
      
    },
    methods: {
      /*添加*/
      onSubmit() {
        let self = this;
        self.$refs.form.validate(valid => {
          if(valid){
            self.loading = true;
            let params = Object.assign({},self.form);
            
            LangApi.add(params, true)
              .then(data => {
                self.loading = false;
                self.$message({
                  message: '恭喜你，添加成功',
                  type: 'success'
                });
                self.dialogFormVisible(true);
              })
              .catch(error => {
                self.loading = false;
              });
          }
        })
      },
  
      /*关闭弹窗*/
      dialogFormVisible(e) {
        if (e) {
          this.$emit('close', {
            type: 'success',
            openDialog: false
          });
        } else {
          this.$emit('close', {
            type: 'error',
            openDialog: false
          });
        }
      }
    }
  };
  </script>
  
  <style></style>
  